import { defineStore } from 'pinia';
import { TOKEN } from '@/constant';
import { getItem } from '@/utils/storage';
export const useUserStore = defineStore('user', {
  state: () => {
    return {
      token: getItem(TOKEN) || '',
      userInfo: {
        avatar:
          'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
      }
    };
  },
  persist: {
    enabled: true,
    strategies: [
      { storage: sessionStorage, paths: ['userInfo'] },
      { storage: localStorage, paths: ['token'] }
    ]
  },
  actions: {
    async set_userInfo() {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({
            name: '呵呵哒',
            id: '123'
          });
        }, 2000);
      });
    },
    async SET_TOKEN(v = 'token_adwaifhialdwa') {
      return new Promise((resolve) => {
        setTimeout(() => {
          this.token = v;
          this.userInfo = { name: '呵呵哒' };
          resolve();
        }, 2000);
      });
    },
    logout() {
      this.token = '';
      location.reload();
    }
  }
});
